package exam.tencent;

class TreeNode {
  int val = 0;
  TreeNode left = null;
  TreeNode right = null;
  public TreeNode(int val) {
    this.val = val;
  }
}

public class Main {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(5);
        root.right = new TreeNode(10);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(5);
        Main test = new Main();
        test.valueOfTree(root);
    }




    public TreeNode valueOfTree (TreeNode root) {
        // write code here
        getVal(root);
        dfs(root);
        return root;
    }

    //计算树的乘积
    private int getVal(TreeNode root){
        if(root == null){
            return 1;
        }

        int leftVal = getVal(root.left);
        int rightVal = getVal(root.right);

        int val = leftVal*rightVal*root.val;


        root.val = val;

        return val;
    }

    //计算0的个数
    private void dfs(TreeNode root){
        if(root == null){
            return;
        }
        dfs(root.left);
        dfs(root.right);
        int count = 0;
        int num = root.val;
        while (num%10 == 0){
            count++;
            num /= 10;
        }
        root.val = count;
    }

    public int[] fun(int n){

        if(n == 2){
            return new int[]{2,1,2};
        }
        int len = n*(n+1)/2;
        int[] ans = new int[len];

        for(int i = 0,j = 0;i<len;){
            int k = j+1;
            while (i<len && k<=n){
                ans[i] = k;
                k++;
                i++;
            }
            j++;
        }
        int val = ans[0];
        ans[0] = ans[len-1];
        ans[len-1] = val;
        return ans;
    }
}
